﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HookupAppForm.Draw.Models
{
    public class DrawEleInfo : INotifyPropertyChanged, ICloneable
    {
        public string uid { get; set; }

        private DrawCommand _DrawCommand;
        public DrawCommand DrawCommand
        {
            get
            {
                return _DrawCommand;
            }
            set
            {
                _DrawCommand = value;
                cc("DrawCommand");
                cc("TextVisible");
                cc("ShapeVisible");
                cc("BorderVisible");
                cc("WidthHeightEnable");
            }
        }

        private bool _IsSlt;
        public bool IsSlt
        {
            get
            {
                return _IsSlt;
            }
            set
            {
                _IsSlt = value;
                cc("IsSlt");
                cc("StrokeShadowVisible");
                cc("StrokeShadow");
            }
        }
        private bool _IsMouseOver;
        public bool IsMouseOver
        {
            get
            {
                return _IsMouseOver;
            }
            set
            {
                _IsMouseOver = value;
                cc("IsMouseOver");
                cc("StrokeShadow");
            }
        }

        public string CurveDataStr
        {
            get
            {
                //Data = "M30,0  S80,280  150,10"    起始点，控制点，结束点

                //如果控制点缺失，则使用中间点
                var ctl_x = Control_1_X;
                var ctl_y = Control_1_Y;
                if (ctl_x == 0 && ctl_y == 0)
                {
                    ctl_x = (LocX + LocX2) * 0.5;
                    ctl_y = (LocY + LocY2) * 0.5;
                }
                return $"M{LocX},{LocY} S{ctl_x},{ctl_y} {LocX2},{LocY2}";
            }
        }
        private double _Control_1_X;
        public double Control_1_X
        {
            get
            {
                return _Control_1_X;
            }
            set
            {
                _Control_1_X = value;
                cc("Control_1_X");
                cc("CurveDataStr");
            }
        }
        private double _Control_1_Y;
        public double Control_1_Y
        {
            get
            {
                return _Control_1_Y;
            }
            set
            {
                _Control_1_Y = value;
                cc("Control_1_Y");
                cc("CurveDataStr");
            }
        }
        private double _LocX;
        public double LocX
        {
            get
            {
                return _LocX;
            }
            set
            {
                _LocX = value;
                cc("LocX");
                cc("CurveDataStr");
            }
        }

        private double _LocX2;
        public double LocX2
        {
            get
            {
                return _LocX2;
            }
            set
            {
                _LocX2 = value;
                cc("LocX2");
                cc("CurveDataStr");

                SizeWidth = Math.Abs(LocX2 - LocX);
            }
        }
        private double _LocY;
        public double LocY
        {
            get
            {
                return _LocY;
            }
            set
            {
                _LocY = value;
                cc("LocY");
                cc("CurveDataStr");
            }
        }

        private double _LocY2;
        public double LocY2
        {
            get
            {
                return _LocY2;
            }
            set
            {
                _LocY2 = value;
                cc("LocY2");
                cc("CurveDataStr");
                SizeHeight = Math.Abs(LocY2 - LocY);
            }
        }


        private double _SizeWidth;
        public double SizeWidth
        {
            get
            {
                return _SizeWidth;
            }
            set
            {
                _SizeWidth = value;
                cc("SizeWidth");
                if (DrawCommand == DrawCommand.圆形
                    || DrawCommand == DrawCommand.矩形
                    )
                {
                    _LocX2 = LocX + value;
                }
            }
        }
        private double _SizeHeight;
        public double SizeHeight
        {
            get
            {
                return _SizeHeight;
            }
            set
            {
                _SizeHeight = value;
                cc("SizeHeight");
                if (DrawCommand == DrawCommand.圆形
                    || DrawCommand == DrawCommand.矩形
                    )
                {
                    _LocY2 = LocY + value;
                }
            }
        }
        private System.Windows.Media.Brush _Stroke = System.Windows.Media.Brushes.White;
        public System.Windows.Media.Brush Stroke
        {
            get
            {
                return _Stroke;
            }
            set
            {
                _Stroke = value;
                cc("Stroke");
            }
        }
        static System.Windows.Media.Brush bs_red_mouseover =
            new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(70, 255, 255, 0));
        public System.Windows.Media.Brush StrokeShadow
        {
            get
            {
                if (IsMouseOver || IsSlt) return bs_red_mouseover;
                else return System.Windows.Media.Brushes.Transparent;
            }

        }
        private System.Windows.Media.SolidColorBrush _Fill = System.Windows.Media.Brushes.Transparent;
        public System.Windows.Media.SolidColorBrush Fill
        {
            get
            {
                return _Fill;
            }
            set
            {
                _Fill = value; 
                cc("Fill");
                cc("FillWidthOpacity");
            }
        }
        private int _FillOpacityPct = 0;
        public int FillOpacityPct
        {
            get
            {
                return _FillOpacityPct;
            }
            set
            {
                _FillOpacityPct = value;
                cc("FillOpacityPct");
                cc("FillWidthOpacity");
            }
        }
        public System.Windows.Media.Brush FillWidthOpacity
        {
            get 
            { 
                return new System.Windows.Media.SolidColorBrush(
                    System.Windows.Media.Color.FromArgb((byte)Math.Min(255, Math.Max(0, (255* (FillOpacityPct)*0.01))), 
                    Fill.Color.R, Fill.Color.G, Fill.Color.B));
            } 
        }

        private string _TextStr = null;
        public string TextStr
        {
            get
            {
                return _TextStr;
            }
            set
            {
                _TextStr = value;
                cc("TextStr");
            }
        }

        private double _FontSize = 12;
        public double FontSize
        {
            get
            {
                return _FontSize;
            }
            set
            {
                _FontSize = value;
                cc("FontSize");
            }
        }
        private double _BorderRadius = 0;
        public double BorderRadius
        {
            get
            {
                return _BorderRadius;
            }
            set
            {
                _BorderRadius = value;
                cc("BorderRadius");
            }
        }
        private bool _MO = false;
        public bool MO
        {
            get
            {
                return _MO;
            }
            set
            {
                _MO = value;
                cc("MO");
            }
        }
        public System.Windows.Visibility TextVisible
        {
            get
            {
                if (DrawCommand == DrawCommand.文字) return System.Windows.Visibility.Visible;
                else return System.Windows.Visibility.Collapsed;
            }
        }
        public System.Windows.Visibility BorderVisible
        {
            get
            {
                if (DrawCommand == DrawCommand.矩形) return System.Windows.Visibility.Visible;
                else return System.Windows.Visibility.Collapsed;
            }
        }

        private bool _DragGoing = false;
        public bool DragGoing
        {
            get
            {
                return _DragGoing;
            }
            set
            {
                _DragGoing = value;
                cc("DragGoing");
                if (value) StrokeShadowVisible = System.Windows.Visibility.Visible;
                else StrokeShadowVisible = System.Windows.Visibility.Collapsed;
            }
        }

        private System.Windows.Visibility _StrokeShadowVisible = System.Windows.Visibility.Visible;
        public System.Windows.Visibility StrokeShadowVisible
        {
            get
            {
                return _StrokeShadowVisible;
            }
            set
            {
                _StrokeShadowVisible = value;
                cc("StrokeShadowVisible");
            }
        }

        public System.Windows.Visibility ShapeVisible
        {
            get
            {
                DrawCommand[] cmds = new DrawCommand[] {
                 DrawCommand.直线,  DrawCommand.矩形, DrawCommand.圆形, DrawCommand.曲线
                };
                if (cmds.Contains(DrawCommand)) return System.Windows.Visibility.Visible;
                else return System.Windows.Visibility.Collapsed;
            }
        }

        /// <summary>
        /// 当类型为path时，宽高只读
        /// </summary>
        public bool WidthHeightEnable
        {
            get
            {
                DrawCommand[] cmds = new DrawCommand[] {
                 DrawCommand.直线,    DrawCommand.曲线
                };
                if (cmds.Contains(DrawCommand)) return false;
                else return true;
            }
        }

        /// <summary>
        /// path 类型使用点1，点2，其他类型使用位置
        /// </summary>
        public System.Windows.Visibility LocVisible
        {
            get
            {
                DrawCommand[] cmds = new DrawCommand[] {
                 DrawCommand.直线,    DrawCommand.曲线
                };
                if (cmds.Contains(DrawCommand)) return System.Windows.Visibility.Collapsed;
                else return System.Windows.Visibility.Visible;
            }
        }


        /// <summary>
        /// path 类型时可见，使用点1，点2，其他类型不可见，
        /// </summary>
        public System.Windows.Visibility LineVisible
        {
            get
            {
                DrawCommand[] cmds = new DrawCommand[] {
                 DrawCommand.直线,    DrawCommand.曲线
                };
                if (cmds.Contains(DrawCommand)) return System.Windows.Visibility.Visible;
                else return System.Windows.Visibility.Collapsed;
            }
        }

        private double _StrokeThickness = 1;
        public double StrokeThickness
        {
            get
            {
                return _StrokeThickness;
            }
            set
            {
                _StrokeThickness = value;
                cc("StrokeThickness");
                cc("StrokeShadowThickness");
                cc("StrokeThicknessScaleResult");
            }
        }
        private double _StrokeThicknessScale = 1;
        public double StrokeThicknessScale
        {
            get
            {
                return _StrokeThicknessScale;
            }
            set
            {
                _StrokeThicknessScale = value;
                //cc("StrokeThicknessScale");
                cc("StrokeThicknessScaleResult");
            }
        }
        public double StrokeThicknessScaleResult
        {
            get
            {
                return StrokeThickness / StrokeThicknessScale;
            }
        }

        public double StrokeShadowThickness
        {
            get
            {
                return StrokeThickness + 20;
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void cc(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }

        public object Clone()
        {
            var copyitem = this.MemberwiseClone() as DrawEleInfo;
            copyitem.StrokeShadowVisible = this.StrokeShadowVisible;
            copyitem.Fill = this.Fill;
            copyitem.Stroke = this.Stroke;
            return copyitem;
        }
    }
}
